import 'package:enterprise_v6_app/app/components/custom_modele/custom_progress_comp_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/common_special_methods.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_home_page/special_personal_data_page/special_personal_data_controller.dart';
import 'package:syncfusion_flutter_charts/charts.dart' as charts;
import 'package:syncfusion_flutter_gauges/gauges.dart';

/// 安全生产-首页-个人数据
class SpecialPersonalDataPage extends StatelessWidget {
  SpecialPersonalDataPage({super.key});

  final controller = Get.put(SpecialPersonalDataController());

  ///生成当前作业统计
  ///[title] 标题
  ///[value] 进度值
  ///[color] 显示颜色
  ///[number] 显示次数
  Widget _buildCurrentJobBar({
    required String title,
    required double value,
    required Color color,
    required String number,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(
          height: 90.w,
          width: 90.w,
          child: SfRadialGauge(
            axes: <RadialAxis>[
              RadialAxis(
                  radiusFactor: 0.85,
                  axisLineStyle: AxisLineStyle(
                      color: const Color(0xFFE0E4EB).withOpacity(0.8),
                      thickness: 0.24,
                      thicknessUnit: GaugeSizeUnit.factor),
                  showLabels: false,
                  showTicks: false,
                  startAngle: 270,
                  endAngle: 270,
                  pointers: <GaugePointer>[
                    RangePointer(
                        pointerOffset: 0.05,
                        width: 0.15,
                        value: value,
                        cornerStyle: CornerStyle.bothCurve,
                        color: color,
                        sizeUnit: GaugeSizeUnit.factor),
                  ],
                  annotations: <GaugeAnnotation>[
                    GaugeAnnotation(
                        widget: Text(
                          '$value%',
                          style: TextStyle(
                              color: color,
                              fontWeight: FontWeight.w400,
                              fontSize: 16.sp),
                        ),
                        positionFactor: 0.15,
                        angle: 90)
                  ])
            ],
          ),
        ),
        AppGaps.vGap4,
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Text(
              title,
              style: TextStyle(
                  color: const Color(0xFFA0A7B5),
                  fontWeight: FontWeight.w400,
                  fontSize: 12.sp),
            ),
            AppGaps.hGap8,
            Text(
              '$number次',
              style: TextStyle(
                  color: color, fontWeight: FontWeight.w400, fontSize: 12.sp),
            )
          ],
        )
      ],
    );
  }

  ///获取历史概况标题
  Widget _getJobSituationTitle(String value) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 12.w),
      child: Row(
        children: [
          SvgPicture.asset(
            AppImages.icon_title,
            width: 20.w,
            height: 20.w,
          ),
          AppGaps.hGap6,
          Text(
            '特殊作业历史概况',
            style: TextStyle(
                color: const Color(0xff262F38),
                height: 1.2,
                fontWeight: FontWeight.w500,
                fontSize: 14.sp),
          ),
          Expanded(
              child: Text(
            '$value次',
            textAlign: TextAlign.right,
            style: TextStyle(
                color: const Color(0xff5C6DFF),
                height: 1.2,
                fontWeight: FontWeight.w500,
                fontSize: 15.sp),
          ))
        ],
      ),
    );
  }

  ///获取历史概况饼图显示
  Widget _getJobSituationView() {
    return charts.SfCircularChart(
      margin: EdgeInsets.zero,
      // legend: Legend(
      //     isVisible: true,
      //     position: LegendPosition.right,
      //     overflowMode: LegendItemOverflowMode.none,
      //     legendItemBuilder:
      //         (String name, dynamic series, dynamic point, int seriesIndex) {
      //       return Container(
      //         width: 100.w,
      //         height: 30.w,
      //         alignment: Alignment.center,
      //         // margin: EdgeInsets.only(top: 7.w),
      //         child: Row(
      //           children: [
      //             Container(
      //               width: 10.w,
      //               height: 10.w,
      //               margin: EdgeInsets.only(right: 5.w),
      //               decoration: BoxDecoration(
      //                   color: controller.chartList[seriesIndex].color,
      //                   borderRadius: BorderRadius.circular(2.w)),
      //             ),
      //             Text(
      //               name,
      //               style: TextStyle(
      //                   fontSize: 12.w,
      //                   height: 1.2,
      //                   fontWeight: FontWeight.w400,
      //                   color: const Color(0xffA0A7B5)),
      //             ),
      //             AppGaps.hGap10,
      //             Expanded(
      //                 child: Text(
      //               '${controller.chartList[seriesIndex].y.toInt()}',
      //               style: TextStyle(
      //                 fontSize: 12.sp,
      //                 height: 1.2,
      //                 fontWeight: FontWeight.w400,
      //                 color: controller.chartList[seriesIndex].color,
      //               ),
      //             )),
      //           ],
      //         ),
      //       );
      //     }),
      series: _getRadiusPieSeries(),
      onTooltipRender: (charts.TooltipArgs args) {
        args.text =
            '${controller.chartList[args.pointIndex!.toInt()].x}:${controller.chartList[args.pointIndex!.toInt()].y.toInt()}';
      },
      tooltipBehavior: charts.TooltipBehavior(enable: true),
    );
  }

  /// 返回饼图样式
  List<charts.PieSeries<ChartData, String>> _getRadiusPieSeries() {
    return <charts.PieSeries<ChartData, String>>[
      charts.PieSeries<ChartData, String>(
        dataSource: controller.chartList,
        xValueMapper: (ChartData data, _) => data.x,
        yValueMapper: (ChartData data, _) => data.y,
        startAngle: 100,
        endAngle: 100,
        pointColorMapper: (ChartData data, _) => data.color,
        //模块显示半径
        pointRadiusMapper: (ChartData data, _) => data.text,
        //数据标签设置
        dataLabelSettings: const charts.DataLabelSettings(
            isVisible: false,
            labelPosition: charts.ChartDataLabelPosition.outside),
      )
    ];
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return ListView(
            padding: EdgeInsets.only(top: 14.w),
            children: [
              CustomTitleChartPage(
                  title: '当前作业',
                  contentPadding:
                      EdgeInsets.only(top: 10.w, bottom: 10.w, left: 5.w),
                  child: SizedBox(
                    height: 120.w,
                    child: ListView(
                      scrollDirection: Axis.horizontal, // 设置横向滚动
                      children: [
                        _buildCurrentJobBar(
                            title: '未进行:',
                            value:
                                CommonSpecialMethods.pieChartValueCalculation(
                              dividend:
                                  controller.workCountEntity.notIssuedCount!,
                              divisor: controller.taskTotal,
                            ),
                            color: const Color(0xFFFF8B8C),
                            number:
                                '${controller.workCountEntity.notIssuedCount!}'),
                        _buildCurrentJobBar(
                            title: '进行中:',
                            value:
                                CommonSpecialMethods.pieChartValueCalculation(
                              dividend: controller.workCountEntity.issuedCount!,
                              divisor: controller.taskTotal,
                            ),
                            color: const Color(0xFF3491FA),
                            number:
                                '${controller.workCountEntity.issuedCount!}'),
                        _buildCurrentJobBar(
                            title: '已完成:',
                            value:
                                CommonSpecialMethods.pieChartValueCalculation(
                              dividend:
                                  controller.workCountEntity.acceptedCount!,
                              divisor: controller.taskTotal,
                            ),
                            color: const Color(0xFF14D2B8),
                            number:
                                '${controller.workCountEntity.acceptedCount!}'),
                      ],
                    ),
                  )),
              AppGaps.vGap20,
              CustomTitleChartPage(
                title: '作业异常',
                child: SizedBox(
                  height: 230.w,
                  child: ListView.builder(
                    itemCount: controller.workHistoryList.length,
                    // shrinkWrap: true,
                    // physics: const NeverScrollableScrollPhysics(),
                    itemBuilder: (BuildContext context, int index) {
                      return CustomProgressComp(
                          bgColor: const Color(0xffEFF7FF),
                          height: 6.w,
                          width: 319.w,
                          value: controller.workHistoryList[index].value,
                          title:
                              controller.workHistoryList[index].ticketTypeName!,
                          num: controller.workHistoryList[index].ticketCount!
                              .toString());
                    },
                  ),
                ),
              ),
              AppGaps.vGap20,
              CustomTitleChartPage(
                  titleView: _getJobSituationTitle(controller.historyTotal),
                  contentPadding: EdgeInsets.zero,
                  child: Column(
                    children: [
                      AppGaps.vGap10,
                      SizedBox(
                        height: 150.w,
                        child: _getJobSituationView(),
                      ),
                      GridView.builder(
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        gridDelegate:
                            const SliverGridDelegateWithFixedCrossAxisCount(
                          // 设置每行显示两个子项
                          crossAxisCount: 2,
                          // 设置每子元素的大小（宽高比）
                          childAspectRatio: 4,
                        ),
                        itemCount: controller.chartList.length,
                        // 你的数据列表长度
                        itemBuilder: (BuildContext context, int index) {
                          final item = controller.chartList[index];
                          return Row(
                            // mainAxisSize: MainAxisSize.min,
                            children: [
                              Container(
                                width: 10.w,
                                height: 10.w,
                                margin: EdgeInsets.only(right: 5.w),
                                decoration: BoxDecoration(
                                    color: item.color,
                                    borderRadius: BorderRadius.circular(2.w)),
                              ),
                              Text(
                                item.x,
                                style: TextStyle(
                                    fontSize: 12.w,
                                    height: 1.2,
                                    fontWeight: FontWeight.w400,
                                    color: const Color(0xffA0A7B5)),
                              ),
                              AppGaps.hGap10,
                              Expanded(
                                  child: Text(
                                '${item.y.toInt()}',
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  height: 1.2,
                                  fontWeight: FontWeight.w400,
                                  color: item.color,
                                ),
                              )),
                            ],
                          );
                        },
                      ),
                      // Wrap(
                      //   // spacing: 2.w, // 主轴(水平)方向间距
                      //   runSpacing: 10.w, // 纵轴（垂直）方向间距
                      //   // alignment: WrapAlignment.start, //沿主轴方向居中
                      //   direction: Axis.horizontal,
                      //   children: controller.chartList.map<Widget>((item) {
                      //     return Row(
                      //       mainAxisSize: MainAxisSize.min,
                      //       children: [
                      //         Container(
                      //           width: 10.w,
                      //           height: 10.w,
                      //           margin: EdgeInsets.only(right: 5.w),
                      //           decoration: BoxDecoration(
                      //               color: item.color,
                      //               borderRadius: BorderRadius.circular(2.w)),
                      //         ),
                      //         Text(
                      //           item.x,
                      //           style: TextStyle(
                      //               fontSize: 12.w,
                      //               height: 1.2,
                      //               fontWeight: FontWeight.w400,
                      //               color: const Color(0xffA0A7B5)),
                      //         ),
                      //         AppGaps.hGap10,
                      //         Text(
                      //           '${item.y.toInt()}',
                      //           style: TextStyle(
                      //             fontSize: 12.sp,
                      //             height: 1.2,
                      //             fontWeight: FontWeight.w400,
                      //             color: item.color,
                      //           ),
                      //         ),
                      //       ],
                      //     );
                      //   }).toList(),
                      // )
                    ],
                  )),
              AppGaps.vGap20,
            ],
          );
        });
  }
}
